<?
include_once('../conn.php');
include_once('../include/func.php');
include("../user_check.php") ;

$db->DELETE("DELETE FROM `".tablepre."session` WHERE  type = '2' and `uid`='".$checkmember[0][2]."' and `v2` < '1'");
$member = $db->select("SELECT * FROM `".tablepre."user` where uid = '".$checkmember[0][2]."'");

	$check_stock_item = $db->select("SELECT v,v2 FROM `".tablepre."session` where uid = '".$checkmember[0][2]."' and type = 2");
	for($i=0;$i<count($check_stock_item);$i++){
		$stock_items = $db->select("SELECT quantity FROM `".tablepre."stock_item` where iid = '".$check_stock_item[$i][0]."'");
		if($stock_items[0][0]<$check_stock_item[$i][1]){
			exit('<script>alert("'.SITE_STOCK_LESS_WARNING.'");  location="../user_cart.php?action=1";</script>');
			}
		}


if($_POST){
	$check_stock_item = $db->select("SELECT v,v2 FROM `".tablepre."session` where uid = '".$checkmember[0][2]."'  and type = 2");
	for($i=0;$i<count($check_stock_item);$i++){
		$stock_items = $db->select("SELECT quantity FROM `".tablepre."stock_item` where iid = '".$check_stock_item[$i][0]."'");
		if($stock_items[0][0]<$check_stock_item[$i][1]){
			exit('<script>alert("'.SITE_STOCK_LESS_WARNING.'");  location="user_cart.php?action=1";</script>');
			}
		}
	
	$RECEIVER_NAME=htmlspecialchars($_POST['RECEIVER_NAME']);
	$RECEIVER_MOB=htmlspecialchars($_POST['RECEIVER_MOB']);
	$RECEIVER_TEL=htmlspecialchars($_POST['RECEIVER_TEL']);
	$RECEIVER_CODE=htmlspecialchars($_POST['RECEIVER_CODE']);
	$RECEIVER_ADDRESS=htmlspecialchars($_POST['RECEIVER_ADDRESS']);
	$PAY_PAYMENT_METHOD=htmlspecialchars($_POST['PAY_PAYMENT_METHOD']);
	$RECEIVER_NOTE=htmlspecialchars($_POST['RECEIVER_NOTE']);
	$RECEIVER_EMAIL=htmlspecialchars($_POST['RECEIVER_EMAIL']);
	$dsn=filetime;
	$db->INSERT("INSERT INTO `".tablepre."order` (`uid`,`dsn`,`rname`,`rmob`,`radd`,`rtel`,`rcode`,`note`,`datetime`,`delete`,`situation`,`payment`,`paid`,`remail`) VALUES ('".$checkmember[0][2]."','".$dsn."','$RECEIVER_NAME','$RECEIVER_MOB','$RECEIVER_ADDRESS','$RECEIVER_TEL','$RECEIVER_CODE','$RECEIVER_NOTE','".datetime."','0','0','$PAY_PAYMENT_METHOD','0','".$RECEIVER_EMAIL."')");
	
	}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CREATE ORDER</title>
<style>
.jc_lable1{
	width:120px; }
body,td,th {
	font-size: 14px;
	color: #333;
}
</style>
<script>
function my_info(){
	document.order.RECEIVER_NAME.value='<? echo mamber($checkmember[0][2],3); ?>';
	document.order.RECEIVER_MOB.value='<? echo mamber($checkmember[0][2],17); ?>';
	document.order.RECEIVER_TEL.value='<? echo mamber($checkmember[0][2],13); ?>';
	document.order.RECEIVER_CODE.value='<? echo mamber($checkmember[0][2],18); ?>';
	document.order.RECEIVER_ADDRESS.value='<? echo mamber($checkmember[0][2],11); ?>';
	document.order.RECEIVER_EMAIL.value='<? echo mamber($checkmember[0][2],12); ?>';
	}
function CheckForm()
{
	if (document.order.RECEIVER_NAME.value.length == 0) {
		alert("<? echo JS_RETURN_RECEIVER_NAME;?>.");
		document.order.RECEIVER_NAME.focus();
		return false;
	}
	if (document.order.RECEIVER_MOB.value.length == 0) {
		alert("<? echo JS_RETURN_RECEIVER_MOB;?>.");
		document.order.RECEIVER_MOB.focus();
		return false;
	}
	if (document.order.RECEIVER_ADDRESS.value.length == 0) {
		alert("<? echo JS_RETURN_RECEIVER_ADDRESS;?>.");
		document.order.RECEIVER_ADDRESS.focus();
		return false;
	}
	
	var obj1=document.getElementsByName('PAY_PAYMENT_METHOD');
	var i=0,chk=0;
	for (i=0;i<obj1.length;i++) if (obj1[i].checked) chk=1;
	if (chk==0){ alert("<? echo JS_RETURN_PAYMENT_METHOD;?>.");  return false;}
	

}
</script>
</head>

<body><div style="width:780px;"><? echo PAY_NOTICE1.parameters($con1=23).PAY_NOTICE2;?><br /><img src="../images/ttfjpg.php?t=<? echo RECEIVER_INFORMATION; ?>&amp;bg=titlebg2.jpg&amp;z=18&amp;x=5&amp;y=22&amp;r=0&amp;b=0&amp;g=0"  /><br />
<? echo JS_RETURN_SENT_TO_ME;?><input name="my_info" type="checkbox" onclick="my_info()" value="1" />
<form  name="order" method="post" action="" onSubmit="return CheckForm();">
  <table width="100%" border="0" cellspacing="2" cellpadding="2">
  <tr>
      <td bgcolor="#F3F3F3"><span class="jc_lable1"><? echo RECEIVER_NAME;?><font color="red">*</font></span></td>
      <td bgcolor="#F3F3F3"><input type="text" name="RECEIVER_NAME"/></td>
      <td bgcolor="#F3F3F3"><? echo RECEIVER_MOB;?><font color="red">*</font></td>
      <td bgcolor="#F3F3F3"><input type="text" name="RECEIVER_MOB" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false"/></td>
      </tr>
    <tr>
      <td><? echo RECEIVER_TEL;?></td>
      <td><input type="text" name="RECEIVER_TEL" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false"/></td>
      <td><? echo RECEIVER_ZIP_CODE;?></td>
      <td><input type="text" name="RECEIVER_CODE" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false"/></td>
      </tr>
    <tr>
      <td bgcolor="#F3F3F3"><? echo RECEIVER_EMAIL;?></td>
      <td colspan="3" bgcolor="#F3F3F3"><input type="text" name="RECEIVER_EMAIL"/></td>
    </tr>
    <tr>
      <td bgcolor="#FFFFFF"><? echo RECEIVER_ADDRESS;?><font color="red">*</font></td>
      <td colspan="3" bgcolor="#FFFFFF"><input name="RECEIVER_ADDRESS" type="text" size="60"/></td>
    </tr>
    <tr>
      <td valign="top" bgcolor="#F3F3F3"><? echo RECEIVER_NOTE;?></td>
      <td colspan="3" bgcolor="#F3F3F3"><textarea name="RECEIVER_NOTE" cols="60" rows="3"></textarea></td>
      </tr>
    <tr>
      <td valign="top" bgcolor="#FFFFFF"><? echo PAY_PAYMENT_METHOD;?><font color="red">*</font></td>
      <td colspan="3" bgcolor="#FFFFFF">
                <label>
            <input name="PAY_PAYMENT_METHOD" type="radio" value="Deposit" checked="checked"  />
        Deposit</label></td>
    </tr>
  </table>

  <table width="100%" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td colspan="6"><img src="../images/ttfjpg.php?t=<? echo CART_REVIEW; ?>&amp;bg=titlebg2.jpg&amp;z=18&amp;x=5&amp;y=22&amp;r=0&amp;b=0&amp;g=0"  /></td>
  </tr>  <? 
  	$cart_session = $db->select("SELECT v3 FROM `".tablepre."session` where `uid` = '".$checkmember[0][2]."' and type = '2' GROUP BY v3");
	for($i=0;$i<count($cart_session);$i++){
		$in_cart_array .= ','.$cart_session[$i][0];
		}
	$in_cart_array = substr($in_cart_array,1);
	$array_stock = $db->select("SELECT name,sid FROM `".tablepre."stock` where `sid` in (".$in_cart_array.")");
	for($i=0;$i<count($array_stock);$i++){
		$count_item = $db->select("SELECT sum(v2) FROM `".tablepre."session` where `v3` = '".$array_stock[$i][1]."' and type = '2' and `uid` = '".$checkmember[0][2]."'");
		if ($count_item[0][0]<minimun($array_stock[$i][1])){
  ?>
    <tr>
    <td colspan="8" bgcolor="#D7D7D7" style="color:red;"><? echo PRODUCT_MINIMUN_ORDER_WARNING.minimun($array_stock[$i][1]);?></td>
    </tr>
  <tr>
  <? }?>
  <tr>
    <td colspan="8" bgcolor="#F3F3F3"><? echo PRODUCT_NAME.'：'.$array_stock[$i][0]; ?> &nbsp; | &nbsp; <? echo PRODUCT_MINIMUN_ORDER.minimun($array_stock[$i][1]).' '.parameters(24)?></td>
    </tr>
  <tr>
  <td><? echo PRODUCT_BATCH_NO;?></td>
    <td><? echo PRODUCT_MODEL; ?></td>
    <td><? echo PRODUCT_MATERIAL; ?></td>
    <td><? echo PRODUCT_COLOR; ?></td>
    <td><? echo PRODUCT_SIZE;?></td>
    <td><? echo PRODUCT_QUANTITY; ?></td>
    <td><? echo PRODUCT_PRICE; ?></td>
    <td><? echo CART_TOTAL; ?></td>
  </tr>
  <?
  
  
  $cart_session_d = $db->select("SELECT v,v2 FROM `".tablepre."session` where `v3` = '".$array_stock[$i][1]."' and type = '2' and `uid` = '".$checkmember[0][2]."'");
  for($k=0;$k<count($cart_session_d);$k++){
	  
	  $arr_list = $db->select("SELECT name,quality1,quality2,quality3,quantity,price,model,unit,iid,batch FROM `".tablepre."stock_item` where iid = '".$cart_session_d[$k][0]."' and sale = '1' ");
	 // print_r($arr_list);
	  /*
	  }
  $arr_list = $db->select("SELECT name,quality1,quality2,quality3,quantity,price,model,unit,iid FROM `".tablepre."stock_item` where sid = '".$array_stock[$i][0]."' and sale = '1' ");
		for($j=0;$j<count($arr_list);$j++) {*/ ?>
        <tr>
        <td><? echo $arr_list[0][9]; ?></td>
        <td><? echo $arr_list[0][6]==null?attachment(1,$array_stock[$i][1],model):$arr_list[0][6]; ?></td>
         <td><? echo $arr_list[0][3]; ?></td>
        <td><? echo $arr_list[0][1]; ?></td>
        <td><? echo $arr_list[0][2]; ?></td>
        <td><? echo $cart_session_d[$k][1]; ?></td>
        <td><del><? echo unit(mround($arr_list[0][5])); ?></del> <? echo '(↓'.vip($checkmember[0][2],2).'%)  '.unit(price_descount($checkmember[0][2],$arr_list[0][5]));?></td>
        <td><? echo unit(price_descount($checkmember[0][2],$arr_list[0][5])*$cart_session_d[$k][1]);
		 $all .= price_descount($checkmember[0][2],$arr_list[0][5])*$cart_session_d[$k][1].',';
		 ?></td>
        </tr>
  <?
  if($_POST){
	  $last_order_id = $db->select("SELECT oid FROM `".tablepre."order` order by oid desc limit 1");
	  $db->INSERT("INSERT INTO `".tablepre."order_item` (`uid`,`oid`,`sid`,`amount`,`price`,`item`) VALUES ('".$checkmember[0][2]."','".$last_order_id[0][0]."','".$array_stock[$i][1]."','".$cart_session_d[$k][1]."','".price_descount($checkmember[0][2],$arr_list[0][5])*$cart_session_d[$k][1]."','".$cart_session_d[$k][0]."')");
	  $db->UPDATE("UPDATE `".tablepre."stock_item`  set quantity = concat(quantity - ".$cart_session_d[$k][1].") where iid = ".$cart_session_d[$k][0]."");

	  
	  }
		}
		
		
  ?>
  
  
  <? }
  if($_POST){
	  $db->UPDATE("UPDATE `".tablepre."order`  set price = '".array_sum(array_filter(explode(',',$all)))."', score = '".floor(array_sum(array_filter(explode(',',$all)))/(vip($checkmember[0][2],4)/vip($checkmember[0][2],3)))."' where `dsn` = '".$dsn."'");
	  $db->UPDATE("UPDATE `".tablepre."session` SET `v`='' WHERE (`uid`='".$checkmember[0][2]."')");  //更新数组
	  $db->DELETE("DELETE FROM `".tablepre."session` WHERE `v3`='".$item."' and type = '2' and `uid`='".$user."'");
	  	header("Location: pay.php?oid=".$last_order_id[0][0] );
	exit;
	  }
   ?>
  </table>

  <input type="submit" name="button" value="<? echo CART_PLACE_ORDER;?>" onclick="return confirm('<? echo PAY_GO;?>')" /><p><br />
</p>
</form></div>
</body>
</html>
